package com.tjbklx1.thread.chap05.demo01;

import java.util.Random;

public class ProducerThread extends Thread {

	private final Random random;
	private final Table table;
	private static int id=0;

	public ProducerThread(String name, long seed, Table table) {
		super(name);
		this.random = new Random(seed);
		this.table = table;
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(random.nextInt(1000));
				String cake = "[ Cake No." + nextId() + " by " + getName() + " ]";
				table.put(cake);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private static synchronized int nextId() {
		return id++;
	}

}
